﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using AnesSystem.Services;
using MongoDB.Bson;
using AnesSystem.ViewModels.Configuration.DataGrid;

namespace AnesSystem.ViewModels
{
    public class RoomsComboboxCtrlViewModel : BaseViewModel<RoomsComboboxCtrlViewModel>
    {
        #region Variables
        private string _seachValue = "";


        private ObservableCollection<ConfigOperationRoomModel> _rooms;
        private ObservableCollection<ConfigOperationRoomModel> _disRooms = new ObservableCollection<ConfigOperationRoomModel>();
        private ConfigOperationRoomModel _curRoom;

        #endregion

        #region Constructor

        public RoomsComboboxCtrlViewModel()
        {
            SelectedRoom = new ConfigOperationRoomModel()
            {
                Name = "请选择手术室"
            };
            InitData();
        }

      

        #endregion

        #region Properties

        public ObservableCollection<ConfigOperationRoomModel> Rooms
        {
            get
            {
                return _rooms;
            }

            set
            {
                _rooms = value;
                RaisePropertyChanged("Rooms");
            }
        }

        public ConfigOperationRoomModel SelectedRoom
        {
            get
            {
                return _curRoom;
            }

            set
            {
                _curRoom = value;
                RaisePropertyChanged("SelectedRoom");
            }
        }

        public ObservableCollection<ConfigOperationRoomModel> DisRooms
        {
            get
            {
                LinkFind();
                return _disRooms;
            }

            set
            {
                _disRooms = value;
                RaisePropertyChanged("DisRooms");
            }
        }

        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("SeachValue");
            }
        }



        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            CongfigOperationRoomService.Instance.Update(sender as ConfigOperationRoomModel);
        }
      
        private void InitData()
        {
            Rooms = DataManager.Instance.ConfigOperationRoomModelList;
        }
        #endregion

        #region Public/Protected Methods
        public void DelConfigOperationRoomModel(ConfigOperationRoomModel configOperationRoomModel)
        {
            if (configOperationRoomModel != null)
            {
                int index = -1;
                for (int i = 0; i < ConfigRoomDataGridViewModel.Instance.ConfigOperationRoomModel.Count; i++)
                {
                    if (ConfigRoomDataGridViewModel.Instance.ConfigOperationRoomModel[i].Code.Equals(configOperationRoomModel.Code))
                    {
                        index = i;
                        break;
                    }
                }
                if (index!=-1)
                {
                    ConfigRoomDataGridViewModel.Instance.ConfigOperationRoomModel.RemoveAt(index);
                }

                int flag = -1;
                for (int i = 0; i < DataManager.Instance.ConfigOperationRoomModelList.Count; i++)
                {
                    if (DataManager.Instance.ConfigOperationRoomModelList[i].Code.Equals(configOperationRoomModel.Code))
                    {
                        flag = i;
                        break;
                    }
                }
                if (flag != -1)
                {
                    DataManager.Instance.ConfigOperationRoomModelList.RemoveAt(flag);
                }
                RaisePropertyChanged("DisRooms");
                ConfigOperationRoomModelService.Instance.DeleteById(configOperationRoomModel.Id);
            }
        }
        public void AddRow()
        {
            ConfigOperationRoomModel model = new ConfigOperationRoomModel();
            model.Code = "room_" + Rooms.Count;
            model.Name = Rooms.Count.ToString();
            DisRooms.Insert(0, model);
            DataManager.Instance.ConfigOperationRoomModelList.Add(model);
            ConfigOperationRoomModelService.Instance.AddUpdate(model);
            model.PropertyChanged += Item_PropertyChanged;
        }

        public void LinkFind()
        {
            _disRooms.Clear();
            List<ConfigOperationRoomModel> lst = DataManager.Instance.ConfigOperationRoomModelList.Where(O =>
            O.Name.Contains(_seachValue)).ToList();
            foreach (var item in lst)
            {
                item.PropertyChanged += Item_PropertyChanged;
                _disRooms.Add(item);
            }
        }
        #endregion

        #region EventHandlers

        #endregion
    }
}
